
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, 2012 and Azure
-- --------------------------------------------------
-- Date Created: 03/08/2014 21:19:06
-- Generated from EDMX file: E:\WorkSpace\Code\Wedn.Net\Micua\Src\Micua.Model\MicuaModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Micua];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_Post_PostMeta]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[M_PostMetas] DROP CONSTRAINT [FK_Post_PostMeta];
GO
IF OBJECT_ID(N'[dbo].[FK_Post_Post]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[M_Posts] DROP CONSTRAINT [FK_Post_Post];
GO
IF OBJECT_ID(N'[dbo].[FK_User_UserMeta]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[M_UserMetas] DROP CONSTRAINT [FK_User_UserMeta];
GO
IF OBJECT_ID(N'[dbo].[FK_User_Post]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[M_Posts] DROP CONSTRAINT [FK_User_Post];
GO
IF OBJECT_ID(N'[dbo].[FK_Category_Category]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[M_Categorys] DROP CONSTRAINT [FK_Category_Category];
GO
IF OBJECT_ID(N'[dbo].[FK_Link_Link]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[M_Links] DROP CONSTRAINT [FK_Link_Link];
GO
IF OBJECT_ID(N'[dbo].[FK_Post_Tag_Post]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[M_PostTagRels] DROP CONSTRAINT [FK_Post_Tag_Post];
GO
IF OBJECT_ID(N'[dbo].[FK_Post_Tag_Tag]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[M_PostTagRels] DROP CONSTRAINT [FK_Post_Tag_Tag];
GO
IF OBJECT_ID(N'[dbo].[FK_Post_Category_Post]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[M_PostCategoryRels] DROP CONSTRAINT [FK_Post_Category_Post];
GO
IF OBJECT_ID(N'[dbo].[FK_Post_Category_Category]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[M_PostCategoryRels] DROP CONSTRAINT [FK_Post_Category_Category];
GO
IF OBJECT_ID(N'[dbo].[FK_Comment_Comment]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[M_Comments] DROP CONSTRAINT [FK_Comment_Comment];
GO
IF OBJECT_ID(N'[dbo].[FK_Post_Comment]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[M_Comments] DROP CONSTRAINT [FK_Post_Comment];
GO
IF OBJECT_ID(N'[dbo].[FK_Comment_CommentMeta]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[M_CommentMetas] DROP CONSTRAINT [FK_Comment_CommentMeta];
GO
IF OBJECT_ID(N'[dbo].[FK_User_Comment]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[M_Comments] DROP CONSTRAINT [FK_User_Comment];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[M_Posts]', 'U') IS NOT NULL
    DROP TABLE [dbo].[M_Posts];
GO
IF OBJECT_ID(N'[dbo].[M_PostMetas]', 'U') IS NOT NULL
    DROP TABLE [dbo].[M_PostMetas];
GO
IF OBJECT_ID(N'[dbo].[M_Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[M_Users];
GO
IF OBJECT_ID(N'[dbo].[M_UserMetas]', 'U') IS NOT NULL
    DROP TABLE [dbo].[M_UserMetas];
GO
IF OBJECT_ID(N'[dbo].[M_Options]', 'U') IS NOT NULL
    DROP TABLE [dbo].[M_Options];
GO
IF OBJECT_ID(N'[dbo].[M_Tags]', 'U') IS NOT NULL
    DROP TABLE [dbo].[M_Tags];
GO
IF OBJECT_ID(N'[dbo].[M_Categorys]', 'U') IS NOT NULL
    DROP TABLE [dbo].[M_Categorys];
GO
IF OBJECT_ID(N'[dbo].[M_Links]', 'U') IS NOT NULL
    DROP TABLE [dbo].[M_Links];
GO
IF OBJECT_ID(N'[dbo].[M_Comments]', 'U') IS NOT NULL
    DROP TABLE [dbo].[M_Comments];
GO
IF OBJECT_ID(N'[dbo].[M_CommentMetas]', 'U') IS NOT NULL
    DROP TABLE [dbo].[M_CommentMetas];
GO
IF OBJECT_ID(N'[dbo].[M_PostTagRels]', 'U') IS NOT NULL
    DROP TABLE [dbo].[M_PostTagRels];
GO
IF OBJECT_ID(N'[dbo].[M_PostCategoryRels]', 'U') IS NOT NULL
    DROP TABLE [dbo].[M_PostCategoryRels];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'M_Posts'
CREATE TABLE [dbo].[M_Posts] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Slug] varchar(256)  NOT NULL,
    [AuthorName] nvarchar(32)  NOT NULL,
    [Title] nvarchar(128)  NOT NULL,
    [PubDate] datetime  NOT NULL,
    [Modified] datetime  NOT NULL,
    [Content] nvarchar(max)  NOT NULL,
    [Excerpt] nvarchar(max)  NOT NULL,
    [Type] tinyint  NOT NULL,
    [IsTop] bit  NOT NULL,
    [Status] tinyint  NOT NULL,
    [CommentStatus] tinyint  NOT NULL,
    [PingStatus] tinyint  NOT NULL,
    [Pinged] varchar(max)  NULL,
    [Password] varchar(32)  NULL,
    [ViewCount] int  NOT NULL,
    [CommentCount] int  NOT NULL,
    [UserId] int  NOT NULL,
    [ParentId] int  NULL
);
GO

-- Creating table 'M_PostMetas'
CREATE TABLE [dbo].[M_PostMetas] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [PostId] int  NOT NULL,
    [Key] varchar(128)  NOT NULL,
    [Value] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'M_Users'
CREATE TABLE [dbo].[M_Users] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Login] varchar(64)  NOT NULL,
    [NickName] nvarchar(32)  NOT NULL,
    [Password] varchar(32)  NOT NULL,
    [Email] varchar(128)  NULL,
    [Link] varchar(128)  NULL,
    [RegDate] datetime  NOT NULL,
    [Status] tinyint  NOT NULL,
    [Role] smallint  NOT NULL,
    [Description] nvarchar(512)  NULL
);
GO

-- Creating table 'M_UserMetas'
CREATE TABLE [dbo].[M_UserMetas] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [UserId] int  NOT NULL,
    [Key] varchar(128)  NOT NULL,
    [Value] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'M_Options'
CREATE TABLE [dbo].[M_Options] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(128)  NOT NULL,
    [Value] nvarchar(max)  NOT NULL,
    [Enabled] bit  NOT NULL
);
GO

-- Creating table 'M_Tags'
CREATE TABLE [dbo].[M_Tags] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(32)  NOT NULL,
    [Slug] varchar(256)  NOT NULL,
    [Count] int  NOT NULL,
    [Description] nvarchar(128)  NOT NULL
);
GO

-- Creating table 'M_Categorys'
CREATE TABLE [dbo].[M_Categorys] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(32)  NOT NULL,
    [Slug] varchar(256)  NOT NULL,
    [Count] int  NOT NULL,
    [Description] nvarchar(128)  NOT NULL,
    [ParentId] int  NULL
);
GO

-- Creating table 'M_Links'
CREATE TABLE [dbo].[M_Links] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Text] nvarchar(32)  NOT NULL,
    [Image] varchar(128)  NOT NULL,
    [Url] varchar(256)  NOT NULL,
    [Title] nvarchar(64)  NULL,
    [Target] varchar(16)  NOT NULL,
    [Rel] nvarchar(32)  NOT NULL,
    [Type] tinyint  NOT NULL,
    [Visible] bit  NOT NULL,
    [Order] smallint  NOT NULL,
    [ParentId] int  NULL
);
GO

-- Creating table 'M_Comments'
CREATE TABLE [dbo].[M_Comments] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [AuthorName] nvarchar(32)  NOT NULL,
    [AuthorEmail] varchar(128)  NOT NULL,
    [AuthorUrl] varchar(128)  NOT NULL,
    [PubDate] datetime  NOT NULL,
    [Content] nvarchar(max)  NOT NULL,
    [Status] tinyint  NOT NULL,
    [UserId] int  NULL,
    [PostId] int  NOT NULL,
    [ParentId] int  NULL
);
GO

-- Creating table 'M_CommentMetas'
CREATE TABLE [dbo].[M_CommentMetas] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [CommentId] int  NOT NULL,
    [Key] varchar(128)  NOT NULL,
    [Value] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'M_PostTagRels'
CREATE TABLE [dbo].[M_PostTagRels] (
    [Posts_Id] int  NOT NULL,
    [Tags_Id] int  NOT NULL
);
GO

-- Creating table 'M_PostCategoryRels'
CREATE TABLE [dbo].[M_PostCategoryRels] (
    [Posts_Id] int  NOT NULL,
    [Categorys_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'M_Posts'
ALTER TABLE [dbo].[M_Posts]
ADD CONSTRAINT [PK_M_Posts]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'M_PostMetas'
ALTER TABLE [dbo].[M_PostMetas]
ADD CONSTRAINT [PK_M_PostMetas]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'M_Users'
ALTER TABLE [dbo].[M_Users]
ADD CONSTRAINT [PK_M_Users]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'M_UserMetas'
ALTER TABLE [dbo].[M_UserMetas]
ADD CONSTRAINT [PK_M_UserMetas]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id], [Name] in table 'M_Options'
ALTER TABLE [dbo].[M_Options]
ADD CONSTRAINT [PK_M_Options]
    PRIMARY KEY CLUSTERED ([Id], [Name] ASC);
GO

-- Creating primary key on [Id] in table 'M_Tags'
ALTER TABLE [dbo].[M_Tags]
ADD CONSTRAINT [PK_M_Tags]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'M_Categorys'
ALTER TABLE [dbo].[M_Categorys]
ADD CONSTRAINT [PK_M_Categorys]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'M_Links'
ALTER TABLE [dbo].[M_Links]
ADD CONSTRAINT [PK_M_Links]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'M_Comments'
ALTER TABLE [dbo].[M_Comments]
ADD CONSTRAINT [PK_M_Comments]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'M_CommentMetas'
ALTER TABLE [dbo].[M_CommentMetas]
ADD CONSTRAINT [PK_M_CommentMetas]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Posts_Id], [Tags_Id] in table 'M_PostTagRels'
ALTER TABLE [dbo].[M_PostTagRels]
ADD CONSTRAINT [PK_M_PostTagRels]
    PRIMARY KEY CLUSTERED ([Posts_Id], [Tags_Id] ASC);
GO

-- Creating primary key on [Posts_Id], [Categorys_Id] in table 'M_PostCategoryRels'
ALTER TABLE [dbo].[M_PostCategoryRels]
ADD CONSTRAINT [PK_M_PostCategoryRels]
    PRIMARY KEY CLUSTERED ([Posts_Id], [Categorys_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [PostId] in table 'M_PostMetas'
ALTER TABLE [dbo].[M_PostMetas]
ADD CONSTRAINT [FK_Post_PostMeta]
    FOREIGN KEY ([PostId])
    REFERENCES [dbo].[M_Posts]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Post_PostMeta'
CREATE INDEX [IX_FK_Post_PostMeta]
ON [dbo].[M_PostMetas]
    ([PostId]);
GO

-- Creating foreign key on [ParentId] in table 'M_Posts'
ALTER TABLE [dbo].[M_Posts]
ADD CONSTRAINT [FK_Post_Post]
    FOREIGN KEY ([ParentId])
    REFERENCES [dbo].[M_Posts]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Post_Post'
CREATE INDEX [IX_FK_Post_Post]
ON [dbo].[M_Posts]
    ([ParentId]);
GO

-- Creating foreign key on [UserId] in table 'M_UserMetas'
ALTER TABLE [dbo].[M_UserMetas]
ADD CONSTRAINT [FK_User_UserMeta]
    FOREIGN KEY ([UserId])
    REFERENCES [dbo].[M_Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_User_UserMeta'
CREATE INDEX [IX_FK_User_UserMeta]
ON [dbo].[M_UserMetas]
    ([UserId]);
GO

-- Creating foreign key on [UserId] in table 'M_Posts'
ALTER TABLE [dbo].[M_Posts]
ADD CONSTRAINT [FK_User_Post]
    FOREIGN KEY ([UserId])
    REFERENCES [dbo].[M_Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_User_Post'
CREATE INDEX [IX_FK_User_Post]
ON [dbo].[M_Posts]
    ([UserId]);
GO

-- Creating foreign key on [ParentId] in table 'M_Categorys'
ALTER TABLE [dbo].[M_Categorys]
ADD CONSTRAINT [FK_Category_Category]
    FOREIGN KEY ([ParentId])
    REFERENCES [dbo].[M_Categorys]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Category_Category'
CREATE INDEX [IX_FK_Category_Category]
ON [dbo].[M_Categorys]
    ([ParentId]);
GO

-- Creating foreign key on [ParentId] in table 'M_Links'
ALTER TABLE [dbo].[M_Links]
ADD CONSTRAINT [FK_Link_Link]
    FOREIGN KEY ([ParentId])
    REFERENCES [dbo].[M_Links]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Link_Link'
CREATE INDEX [IX_FK_Link_Link]
ON [dbo].[M_Links]
    ([ParentId]);
GO

-- Creating foreign key on [Posts_Id] in table 'M_PostTagRels'
ALTER TABLE [dbo].[M_PostTagRels]
ADD CONSTRAINT [FK_Post_Tag_Post]
    FOREIGN KEY ([Posts_Id])
    REFERENCES [dbo].[M_Posts]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Tags_Id] in table 'M_PostTagRels'
ALTER TABLE [dbo].[M_PostTagRels]
ADD CONSTRAINT [FK_Post_Tag_Tag]
    FOREIGN KEY ([Tags_Id])
    REFERENCES [dbo].[M_Tags]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Post_Tag_Tag'
CREATE INDEX [IX_FK_Post_Tag_Tag]
ON [dbo].[M_PostTagRels]
    ([Tags_Id]);
GO

-- Creating foreign key on [Posts_Id] in table 'M_PostCategoryRels'
ALTER TABLE [dbo].[M_PostCategoryRels]
ADD CONSTRAINT [FK_Post_Category_Post]
    FOREIGN KEY ([Posts_Id])
    REFERENCES [dbo].[M_Posts]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Categorys_Id] in table 'M_PostCategoryRels'
ALTER TABLE [dbo].[M_PostCategoryRels]
ADD CONSTRAINT [FK_Post_Category_Category]
    FOREIGN KEY ([Categorys_Id])
    REFERENCES [dbo].[M_Categorys]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Post_Category_Category'
CREATE INDEX [IX_FK_Post_Category_Category]
ON [dbo].[M_PostCategoryRels]
    ([Categorys_Id]);
GO

-- Creating foreign key on [ParentId] in table 'M_Comments'
ALTER TABLE [dbo].[M_Comments]
ADD CONSTRAINT [FK_Comment_Comment]
    FOREIGN KEY ([ParentId])
    REFERENCES [dbo].[M_Comments]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Comment_Comment'
CREATE INDEX [IX_FK_Comment_Comment]
ON [dbo].[M_Comments]
    ([ParentId]);
GO

-- Creating foreign key on [PostId] in table 'M_Comments'
ALTER TABLE [dbo].[M_Comments]
ADD CONSTRAINT [FK_Post_Comment]
    FOREIGN KEY ([PostId])
    REFERENCES [dbo].[M_Posts]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Post_Comment'
CREATE INDEX [IX_FK_Post_Comment]
ON [dbo].[M_Comments]
    ([PostId]);
GO

-- Creating foreign key on [CommentId] in table 'M_CommentMetas'
ALTER TABLE [dbo].[M_CommentMetas]
ADD CONSTRAINT [FK_Comment_CommentMeta]
    FOREIGN KEY ([CommentId])
    REFERENCES [dbo].[M_Comments]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Comment_CommentMeta'
CREATE INDEX [IX_FK_Comment_CommentMeta]
ON [dbo].[M_CommentMetas]
    ([CommentId]);
GO

-- Creating foreign key on [UserId] in table 'M_Comments'
ALTER TABLE [dbo].[M_Comments]
ADD CONSTRAINT [FK_User_Comment]
    FOREIGN KEY ([UserId])
    REFERENCES [dbo].[M_Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_User_Comment'
CREATE INDEX [IX_FK_User_Comment]
ON [dbo].[M_Comments]
    ([UserId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------